$infile = $ARGV[0];
open(INFILE, $infile) || die("Could not open file $infile\nError: $!");

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

# Taken from Nokia Developer device specifications:
# https://www.developer.nokia.com/Devices/Device_specifications/
# There's no way of knowing if a Symbian^3 device has been updated 
# to Anna or Belle or Klickerklok so the best we can do is to 
# categorise according to OS at launch.

my @SymbianBelle = (
				"Nokia 500 Belle",
				"Nokia 600",
				"Nokia 603",
				"Nokia 700",
				"Nokia 701",
				"Nokia 808 Pureview",
				"Nokia C6-01 Belle",
				"Nokia C7-00 Belle",
				"Nokia E6-00 Belle",
				"Nokia E7-00 Belle",
				"Nokia N8-00 Belle",
				"Nokia X7-00 Belle"
				);

my $SymbianBellefound   = 0;
my $SymbianBellepercent = 0;

my @SymbianAnna = ("Nokia 500",
				"Nokia Oro",
				"Nokia E6-00",
				"Nokia X7-00");

my $SymbianAnnafound   = 0;
my $SymbianAnnapercent = 0;

my @Symbian3 = ("Nokia C6-01",
				"Nokia C7 Astound",
				"Nokia C7-00",
				"Nokia E7-00",
				"Nokia N8-00",
				"Nokia Oro Belle",
				"Vertu Constellation T",
				"Symbian3");

my $Symbian3found   = 0;
my $Symbian3percent = 0;

my @Symbian1 = ("Nokia 5228",
				"Nokia 5230",
				"Nokia 5230 Nuron",
				"Nokia 5233",
				"Nokia 5235",
				"Nokia 5235 Ovi Music Unlimited",
				"Nokia 5250",
				"Nokia 5530 XpressMusic",
				"Nokia 5800 XpressMusic",
				"Nokia C5-03",
				"Nokia C5-04",
				"Nokia C6-00",
				"Nokia N97",
				"Nokia N97 mini",
				"Nokia X6",
				"Nokia X6-00");

my $Symbian1found   = 0;
my $Symbian1percent = 0;

my @S603rdFP2 = ("Nokia 5320 XpressMusic",
				"Nokia 5630 XpressMusic",
				"Nokia 5730 XpressMusic",
				"Nokia 6210 Navigator",
				"Nokia 6220 classic",
				"Nokia 6650 fold",
				"Nokia 6700 slide",
				"Nokia 6710 Navigator",
				"Nokia 6720 classic",
				"Nokia 6730 classic",
				"Nokia 6760 slide",
				"Nokia 6788",
				"Nokia 6788i",
				"Nokia 6790 Surge",
				"Nokia 6790 slide",
				"Nokia C5-00",
				"Nokia C5-01",
				"Nokia E5-00",
				"Nokia E52",
				"Nokia E55",
				"Nokia E71x",
				"Nokia E72",
				"Nokia E73",
				"Nokia E73 Mode",
				"Nokia E75",
				"Nokia N78",
				"Nokia N79",
				"Nokia N85",
				"Nokia N86 8MP",
				"Nokia N96",
				"Nokia N96-3",
				"Nokia N96-3 NAM",
				"Nokia X5-00",
				"Vertu Constellation Quest",
				"Nokia X5-01");

my $S603rdFP2found   = 0;
my $S603rdFP2percent = 0;

my @S603rdFP1 = ("Nokia 5700 XpressMusic",
				"Nokia 6110 Navigator",
				"Nokia 6120 classic",
				"Nokia 6121 classic",
				"Nokia 6124 classic",
				"Nokia 6290",
				"Nokia E51",
				"Nokia E63",
				"Nokia E66",
				"Nokia E71",
				"Nokia E90 Communicator",
				"Nokia N76",
				"Nokia N81",
				"Nokia N81 8GB",
				"Nokia N82",
				"Nokia N95",
				"Nokia N95 8GB",
				"Nokia N95-3 NAM");

my $S603rdFP1found   = 0;
my $S603rdFP1percent = 0;

my @S603rdIR = ("Nokia 3250",
				"Nokia 5500 Sport",
				"Nokia E50",
				"Nokia E60",
				"Nokia E61",
				"Nokia E61i",
				"Nokia E62",
				"Nokia E65",
				"Nokia E70",
				"Nokia N71",
				"Nokia N73",
				"Nokia N75",
				"Nokia N77",
				"Nokia N80",
				"Nokia N80 Internet Edition",
				"Nokia N91",
				"Nokia N91 8GB",
				"Nokia N92",
				"Nokia N93",
				"Nokia N93i");

my $S603rdIRfound   = 0;
my $S603rdIRpercent = 0;

my $unknownfound   = 0;
my $unknownpercent = 0;

while (<INFILE>) {
	chomp;
	my ($rank, $phone, $percent) = split('\t');
	$phone = trim($phone);
	$percent =~ s/[\s]//g;
	#print "$rank--$phone--$percent--\n";
	$totalfound++;
	$totalpercent += $percent;
	
	if (grep /^$phone$/i,  @SymbianBelle) {
		print "$rank\t$percent\tSymbian Belle\t$phone\n";
		$SymbianBellefound++;
		$SymbianBellepercent += $percent;
	}
	elsif (grep /^$phone$/i,  @SymbianAnna) {
		print "$rank\t$percent\tSymbian Anna\t$phone\n";
		$SymbianAnnafound++;
		$SymbianAnnapercent += $percent;
	}
	elsif (grep /^$phone$/i,  @Symbian3) {
		print "$rank\t$percent\tSymbian^3\t$phone\n";
		$Symbian3found++;
		$Symbian3percent += $percent;
	} 
	elsif (grep /^$phone$/i,  @Symbian1) {
		print "$rank\t$percent\tSymbian^1\t$phone\n";
		$Symbian1found++;
		$Symbian1percent += $percent;
	}
	elsif (grep /^$phone$/i,  @S603rdFP2) {
		print "$rank\t$percent\tS60 3.2 \t$phone\n";
		$S603rdFP2found++;
		$S603rdFP2percent += $percent;
	}
	elsif (grep /^$phone$/i,  @S603rdFP1) {
		print "$rank\t$percent\tS60 3.1 \t$phone\n";
		$S603rdFP1found++;
		$S603rdFP1percent += $percent;
	}
	elsif (grep /^$phone$/i,  @S603rdIR) {
		print "$rank\t$percent\tS60 3.0 \t$phone\n";
		$S603rdIRfound++;
		$S603rdIRpercent += $percent;
	}
	else {
		print "$rank\t$percent\tUNKNOWN \t$phone\n";
		$unknownfound++;
		$unknownpercent += $percent;
	}
}
close(INFILE);

my $totalS603rdfound = $S603rdFP2found + $S603rdFP1found + $S603rdIRfound;
my $totalS603rdpercent = $S603rdFP2percent + $S603rdFP1percent + $S603rdIRpercent;

print "=============================\n";
print "$SymbianBellefound Symbian Belle models:\t$SymbianBellepercent%\n";
print "$SymbianAnnafound Symbian Anna models:\t$SymbianAnnapercent%\n";
print "$Symbian3found Symbian^3 models:\t$Symbian3percent%\n";
print "$Symbian1found Symbian^1 models:\t$Symbian1percent%\n";
print "$totalS603rdfound S60 3rd edition:\t$totalS603rdpercent%\n";
print "-----------------------------\n";
print "$S603rdFP2found S60 3.2 models:\t$S603rdFP2percent%\n";
print "$S603rdFP1found S60 3.1 models:\t$S603rdFP1percent%\n";
print "$S603rdIRfound S60 3.0 models:\t$S603rdIRpercent%\n";
print "-----------------------------\n";
print "$unknownfound unknown models:\t$unknownpercent%\n";
print "=============================\n";
print "$totalfound total models:\t$totalpercent%\n";
print "=============================\n";


# Disregard the unknown so total = 100%
my $fixfactor = 100 / (100 - $unknownpercent);

$SymbianBellepercent *= $fixfactor;
$SymbianAnnapercent *= $fixfactor;
$Symbian3percent *= $fixfactor;
$Symbian1percent *= $fixfactor;
$totalS603rdpercent *= $fixfactor;
$S603rdFP2percent *= $fixfactor;
$S603rdFP1percent *= $fixfactor;
$S603rdIRpercent *= $fixfactor;

print "\nIgnoring the unknown:\n";
print "=============================\n";
printf "$SymbianBellefound Symbian Belle models:\t%.1f%\n", $SymbianBellepercent;
printf "$SymbianAnnafound Symbian Anna models:\t%.1f%\n", $SymbianAnnapercent;
printf "$Symbian3found Symbian^3 models:\t%.1f%\n", $Symbian3percent;
printf "$Symbian1found Symbian^1 models:\t%.1f%\n", $Symbian1percent;
printf "$totalS603rdfound S60 3rd ed. models:\t%.f%\n", $totalS603rdpercent;
print "-----------------------------\n";
printf "$S603rdFP2found S60 3.2 models:\t%.1f%\n", $S603rdFP2percent;
printf "$S603rdFP1found S60 3.1 models:\t%.1f%\n", $S603rdFP1percent;
printf "$S603rdIRfound S60 3.0 models:\t%.1f%\n", $S603rdIRpercent;
print "=============================\n";
$totalfound -= $unknownfound;
$totalpercent = $SymbianBellepercent
			  + $SymbianAnnapercent
			  + $Symbian3percent
			  + $Symbian1percent
			  + $S603rdFP2percent
			  + $S603rdFP1percent
			  + $S603rdIRpercent;
printf "%d total models:\t%.1f%\n", $totalfound, $totalpercent;
print "=============================\n";


# Write the data to a file so it can be drawn as a stacked line graph

if (-e "ovid.data") {
	# '>>', the file is opened for appending, being created if necessary
	open(OUTFILE, ">>ovid.data") || die "Can't open the output file\nError: $!";
} else {
	# '>', the file is truncated and opened for output, being created if necessary
	open(OUTFILE, ">ovid.data") || die "Can't open the output file\nError: $!";
	print OUTFILE  "Month\tSymbian_Belle\tSymbian_Anna\tSymbian^3\tSymbian^1\tS60_3.2\tS60_3.1\tS60_3.0\n";
}

my $stackedSymbianAnnapercent = $SymbianBellepercent + $SymbianAnnapercent;
my $stackedSymbian3percent = $stackedSymbianAnnapercent + $Symbian3percent;
my $stackedSymbian1percent = $stackedSymbian3percent + $Symbian1percent;
my $stackedS603rdFP2percent = $stackedSymbian1percent + $S603rdFP2percent;
my $stackedS603rdFP1percent = $stackedS603rdFP2percent + $S603rdFP1percent;
# my $stackedS603rdIRpercent = $stackedS603rdFP1percent + $S603rdIRpercent;
# Smooth off the rounding errors from adding rounded percentages:
my $stackedS603rdIRpercent = 100;

print OUTFILE  "$infile\t$SymbianBellepercent%\t$stackedSymbianAnnapercent%\t$stackedSymbian3percent%\t$stackedSymbian1percent%\t$stackedS603rdFP2percent%\t$stackedS603rdFP1percent%\t$stackedS603rdIRpercent%\n";
close(OUTFILE);
